//
//  WKResetPasswordViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/2.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKResetPasswordViewController.h"
#import "WKBaseHeader.h"

@interface WKResetPasswordViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneNum;//电话号码
@property (weak, nonatomic) IBOutlet UITextField *code;//验证码
@property (weak, nonatomic) IBOutlet UIButton *sendCodeBtn;//发送验证码按钮
@property (weak, nonatomic) IBOutlet UITextField *password1;//密码
@property (weak, nonatomic) IBOutlet UITextField *password2;//再次确认密码
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;//确认按钮

@property (nonatomic,strong) NSTimer *codeTimer;//验证码总时间
@property (nonatomic,assign) int timeCount;//获取验证吗可变的数字

@end

@implementation WKResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self setNavigationView];
    [self layoutUI];
}
-(void)delegate{
    self.phoneNum.delegate=self;
    self.code.delegate=self;
    self.password1.delegate=self;
    self.password2.delegate=self;
}
- (void)setNavigationView {
    [super setNavigationView];
    if(self.topNumber==0){
    self.navigationView.topTitleLabel.text = @"忘记密码";
    }
    else{
       self.navigationView.topTitleLabel.text = @"注册";
        self.password1.placeholder=@"请输入密码";
        self.sureBtn.text=@"注册";
    }
}

- (void)layoutUI {
    [self.sendCodeBtn setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
    
    self.sureBtn.backgroundColor = AppManager.themeColor;
    self.sureBtn.titleLabel.font = AppManager.btnFont;
    self.sureBtn.layer.cornerRadius = 5;
    self.sureBtn.layer.masksToBounds = YES;

}

#pragma mark - 点击获取验证码
- (IBAction)clickSendCodeBtn:(id)sender {
   
    DLog(@"%@",self.phoneNum.text);
    if ([self.phoneNum.text isEqualToString:@""] || [self.phoneNum.text length]==0){
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入手机号/邮箱" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }
    else if([self inputShouldNumber:self.phoneNum.text]==YES&&[self.phoneNum.text length]<11){
        
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入正确的手机号/邮箱" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }
    else{
    
        self.sendCodeBtn.enabled = NO;
        self.timeCount = 60;
        self.codeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTimeOnButton) userInfo:nil repeats:YES];
        [self getCodeNetworking];
    }
}

//60秒倒计时
-(void)countDownTimeOnButton{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%d s",self.timeCount - 1] forState:UIControlStateNormal];
        self.timeCount -= 1;
        if (self.timeCount <= 1) {
            [self.codeTimer invalidate];
            self.codeTimer = nil;
            self.sendCodeBtn.enabled = YES;
            [self.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        }
    });
}

//获取验证码网络请求
- (void)getCodeNetworking {
  
        [httpManager getCodeWithPhoneNum:self.phoneNum.text success:^(NSDictionary *result) {
        DLog(@"result=%@",result);
    
        } failure:^(NSError *error) {
    
        }];
    
}
//判断输入字符是否都是数字
- (BOOL)inputShouldNumber:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}


#pragma mark - 点击确认
- (IBAction)clickSure:(id)sender {

    if ([self.phoneNum.text isEqualToString:@""] || [self.phoneNum.text length]==0){
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入手机号/邮箱" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }
    else if([self inputShouldNumber:self.phoneNum.text]==YES&&[self.phoneNum.text length]<11){
        
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入正确的手机号/邮箱" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }
    else if([self.code.text isEqualToString:@""] || [self.code.text length]==0){
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入验证码" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }
    else if([self.password1.text isEqualToString:@""] || [self.password1.text length]==0){
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"请输入密码" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }
 
    
    else if(![self.password1.text isEqualToString:self.password2.text]){
        DLog(@"%@",self.password1.text);
        DLog(@"%@",self.password2.text);
        UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:@"密码不一致" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alt animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }

    else{
   
        [httpManager registerWithPhoneNum:self.phoneNum.text code:self.code.text password:self.password1.text success:^(NSDictionary *result) {
        
            DLog(@"result=%@",result);
            NSDictionary *errors=[result objectForKey:@"errors"];
            NSString *message=[errors objectForKey:@"message"];
    
            if(message!=nil){
          
                UIAlertController *alt=[UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
           
                [self presentViewController:alt animated:YES completion:nil];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
          //DLog(@"%@",message);       
            }
            else{
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(NSError *error) {


        }];

    }

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_phoneNum resignFirstResponder];
    [_code resignFirstResponder];
    [_password1 resignFirstResponder];
    [_password2 resignFirstResponder];
}

@end
